package com.whately.core.base.exception;

public class ServiceException extends RuntimeException {
    private static final long serialVersionUID = -3434467003214522941L;
    private final ErrorCode serviceError;
    private final int code;
    private final String errorMessage;
    private String[] params;
    private boolean ignorePrintLogDetailFlag;

    public ServiceException(int code, String message) {
        this(code, message, false);
    }

    public ServiceException(int code, String message, boolean ignorePrintLogDetailFlag) {
        this.code = code;
        this.errorMessage = message;
        this.ignorePrintLogDetailFlag = ignorePrintLogDetailFlag;
        this.serviceError = new BaseError(code,message,ignorePrintLogDetailFlag);
    }

    public ServiceException(ErrorCode serviceError,  Object... params) {
        this.code = serviceError.getCode();
        this.errorMessage = serviceError.getMessage(params);
        this.ignorePrintLogDetailFlag = serviceError.isIgnorePrintErrorDetailFlag();
        this.serviceError = serviceError;
    }


    public ServiceException(ErrorCode serviceError, Throwable cause) {
        super(serviceError.getMessage(), cause);
        this.code = serviceError.getCode();
        this.errorMessage = serviceError.getMessage();
        this.ignorePrintLogDetailFlag = serviceError.isIgnorePrintErrorDetailFlag();
        this.serviceError = serviceError;
    }


    public void setParams(String... params) {
        this.params = params;
    }

    public String[] getParams() {
        return this.params;
    }

    public ErrorCode getServiceError() {
        return this.serviceError;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return "[" + this.code + ":" + this.errorMessage + "]";
    }

    public String getErrorMessage() {
        return this.errorMessage;
    }

    public boolean isIgnorePrintLogDetailFlag() {
        return ignorePrintLogDetailFlag;
    }
}
